/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.utn.tpsimulacion02.UI;

import com.utn.tpsimulacion02.ChartManager;
import com.utn.tpsimulacion02.LibNormal;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author aaltamir
 */
public class VentanaNormal extends javax.swing.JFrame {

    private List<Double> values = new ArrayList<Double>();
    /**
     * Creates new form VentanaNormal
     */
    public VentanaNormal() {
        initComponents();
        this.setTitle("Calcular Distribucion Normal");
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.jbGenerarChart.setEnabled(false);
    }
    private boolean esPar(int x){
        
        return  (x % 2 == 0) ? true: false;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bgTipo = new javax.swing.ButtonGroup();
        jPanelTipo = new javax.swing.JPanel();
        jrbBoxMuller = new javax.swing.JRadioButton();
        jrbConvolucion = new javax.swing.JRadioButton();
        jlMedia = new javax.swing.JLabel();
        jtMedia = new javax.swing.JTextField();
        jlCantNumeros = new javax.swing.JLabel();
        jtCantNumeros = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlListNumeros = new javax.swing.JList();
        jbLimpiar = new javax.swing.JButton();
        jbGenerarNumeros = new javax.swing.JButton();
        jbGenerarChart = new javax.swing.JButton();
        jlDesviacionEstandar = new javax.swing.JLabel();
        jtDesviacionEstandar = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanelTipo.setBorder(javax.swing.BorderFactory.createTitledBorder("Tipo"));

        bgTipo.add(jrbBoxMuller);
        jrbBoxMuller.setText("Box Muller");
        jrbBoxMuller.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbBoxMullerActionPerformed(evt);
            }
        });

        bgTipo.add(jrbConvolucion);
        jrbConvolucion.setSelected(true);
        jrbConvolucion.setText("Convolucion");
        jrbConvolucion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbConvolucionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelTipoLayout = new javax.swing.GroupLayout(jPanelTipo);
        jPanelTipo.setLayout(jPanelTipoLayout);
        jPanelTipoLayout.setHorizontalGroup(
            jPanelTipoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTipoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelTipoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jrbBoxMuller)
                    .addComponent(jrbConvolucion))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelTipoLayout.setVerticalGroup(
            jPanelTipoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTipoLayout.createSequentialGroup()
                .addComponent(jrbBoxMuller)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jrbConvolucion))
        );

        jlMedia.setText("Media");

        jlCantNumeros.setText("Cantidad De Numeros");

        jScrollPane1.setViewportView(jlListNumeros);

        jbLimpiar.setText("Limpiar");
        jbLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbLimpiarActionPerformed(evt);
            }
        });

        jbGenerarNumeros.setText("Generar Numeros");
        jbGenerarNumeros.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbGenerarNumerosActionPerformed(evt);
            }
        });

        jbGenerarChart.setText("Generar Grafico");
        jbGenerarChart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbGenerarChartActionPerformed(evt);
            }
        });

        jlDesviacionEstandar.setText("Desviacion Estandar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jbLimpiar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbGenerarChart, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbGenerarNumeros, javax.swing.GroupLayout.Alignment.LEADING))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlCantNumeros)
                            .addComponent(jPanelTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jlMedia)
                            .addComponent(jlDesviacionEstandar))
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtMedia, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
                            .addComponent(jtCantNumeros)
                            .addComponent(jtDesviacionEstandar))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(17, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanelTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jlCantNumeros)
                            .addComponent(jtCantNumeros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jlMedia)
                            .addComponent(jtMedia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jlDesviacionEstandar)
                            .addComponent(jtDesviacionEstandar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbGenerarNumeros)
                        .addGap(18, 18, 18)
                        .addComponent(jbLimpiar)
                        .addGap(18, 18, 18)
                        .addComponent(jbGenerarChart)
                        .addGap(46, 46, 46))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbGenerarNumerosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbGenerarNumerosActionPerformed
        int cantidad = 0;
        
        if(this.jtCantNumeros.getText().isEmpty()){
            JOptionPane.showMessageDialog(this,"La Cantidad de numeros a generar puede estar vacia");
            return;
        }
        try{
            cantidad =Integer.parseInt(this.jtCantNumeros.getText()) ;
        }catch (NumberFormatException e){
            JOptionPane.showMessageDialog(this, "El valor ingresado no es un numero");
            return;
        }
        
        if(this.jrbBoxMuller.isSelected()){
            if (!esPar(cantidad)){
                JOptionPane.showMessageDialog(this, "El valor ingresado no es un debe se impar en Box Muller");
            return;
            }
            values = LibNormal.generarNumerosBoxMuller(cantidad);  
            
        }
        else{
            if(this.jtMedia.getText().isEmpty()|| this.jtDesviacionEstandar.getText().isEmpty()){
                JOptionPane.showMessageDialog(this,"Complete los campos vacios");
                return;
            }
            int ds = 0;
            int media = 0;
            try{
                media = Integer.parseInt(this.jtMedia.getText());
                ds = Integer.parseInt(this.jtDesviacionEstandar.getText());
            } catch(NumberFormatException ex){
                JOptionPane.showMessageDialog(this,"El valor ingresado no es un numero");
                return;
            }
            values = LibNormal.generarNumerosConvolucion(cantidad, media, ds);           
        }
        this.jlListNumeros.setListData(values.toArray());
        this.jbGenerarChart.setEnabled(true);
        
    }//GEN-LAST:event_jbGenerarNumerosActionPerformed

    private void clean(){
       this.jtCantNumeros.setText("");
       this.jtMedia.setText("");
       this.jtDesviacionEstandar.setText("");
       this.jlListNumeros.setModel(new DefaultListModel());
        this.jbGenerarChart.setEnabled(false);
        this.values.clear();
    }
    private void jbLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbLimpiarActionPerformed
       this.clean();
       
    }//GEN-LAST:event_jbLimpiarActionPerformed

    private void jrbBoxMullerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbBoxMullerActionPerformed
        this.jtDesviacionEstandar.setText("");
        this.jtDesviacionEstandar.setEnabled(false);
        this.jtMedia.setText("");
        this.jtMedia.setEnabled(false);
    }//GEN-LAST:event_jrbBoxMullerActionPerformed

    private void jrbConvolucionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbConvolucionActionPerformed
       this.jtDesviacionEstandar.setEnabled(true);
        this.jtMedia.setEnabled(true);
    }//GEN-LAST:event_jrbConvolucionActionPerformed

    private void jbGenerarChartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbGenerarChartActionPerformed
        String str = JOptionPane.showInputDialog(this, "Ingrese la Cantidad de Intervalos que desea", "Generacion de Grafico", 1);
       int cantIntervalos =0;
       if(str !=null){
           try{
               cantIntervalos = Integer.parseInt(str);
           } catch(NumberFormatException e){
               JOptionPane.showMessageDialog(this,"La cantidad de intervalos debe ser un numero entero");
               return;
           }
            ChartManager.generarChart(cantIntervalos,values);
       }
    }//GEN-LAST:event_jbGenerarChartActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bgTipo;
    private javax.swing.JPanel jPanelTipo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbGenerarChart;
    private javax.swing.JButton jbGenerarNumeros;
    private javax.swing.JButton jbLimpiar;
    private javax.swing.JLabel jlCantNumeros;
    private javax.swing.JLabel jlDesviacionEstandar;
    private javax.swing.JList jlListNumeros;
    private javax.swing.JLabel jlMedia;
    private javax.swing.JRadioButton jrbBoxMuller;
    private javax.swing.JRadioButton jrbConvolucion;
    private javax.swing.JTextField jtCantNumeros;
    private javax.swing.JTextField jtDesviacionEstandar;
    private javax.swing.JTextField jtMedia;
    // End of variables declaration//GEN-END:variables
}
